package com.i2863.receipt.util;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
 * @author xuweiwei
 * @date 2018/6/11
 */
public class CommonPage implements Pageable {
    private int page;
    private int limit;
    private int count;

    public CommonPage(int page, int limit,int count) {
        this.page = page;
        this.limit = limit;
        this.count=count;
    }

    @Override
    public int getPageNumber() {
        return page;
    }

    @Override
    public int getPageSize() {
        return limit;
    }

    @Override
    public int getOffset() {
        return (page-1)*limit;
    }

    @Override
    public Sort getSort() {
        return null;
    }

    @Override
    public Pageable next() {
        return new CommonPage(page+1,limit,count);
    }

    @Override
    public Pageable previousOrFirst() {
        if(page==1){
            return this;
        }else{
            return new CommonPage(page-1,limit,count);
        }
    }

    @Override
    public Pageable first() {
        return new CommonPage(1,limit,count);
    }

    @Override
    public boolean hasPrevious() {
        return page>1;
    }


    public int getPageCount(){
        return (int) Math.ceil(1.0*count/limit);
    }
}
